package com.springboot.framework.controller;

import com.springboot.framework.annotation.ACS;
import com.springboot.framework.controller.request.*;
import com.springboot.framework.exception.BusinessException;
import com.springboot.framework.model.UserAddressModel;
import com.springboot.framework.service.BuyerAddressService;
import com.springboot.framework.service.MapService;
import com.springboot.framework.utils.ResponseVoUtil;
import com.springboot.framework.vo.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Api(description = "【买家】收货地址")
@RestController
@RequestMapping("/userAddress")
public class BuyerAddressController {
    @Resource
    private BuyerAddressService buyerAddressService;

    @Resource
    private MapService mapService;

    @ACS(allowAnonymous = true)
    @ApiOperation(value = "获取list", notes = "")
    @GetMapping("/list")
    public ResponseVO<List<UserAddressModel>> list(@RequestParam String openId) {
        return ResponseVoUtil.success(buyerAddressService.list(openId));
    }

    @ACS(allowAnonymous = true)
    @ApiOperation(value = "新增", notes = "")
    @PostMapping("/insert")
    public ResponseVO<UserAddressModel> insert(@RequestBody @Valid UserAddressInsert bean, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(bindingResult.getFieldError().getDefaultMessage());
        }
        UserAddressModel userAddressModel = new UserAddressModel();
        BeanUtils.copyProperties(bean, userAddressModel);
        // 解析地址为经纬度
        setGeocode(userAddressModel);
        return ResponseVoUtil.success(buyerAddressService.insert(userAddressModel));
    }

    @ACS(allowAnonymous = true)
    @ApiOperation(value = "新增", notes = "")
    @PostMapping("/insertForLatitudeAndLongitude")
    public ResponseVO<UserAddressModel> insertForLatitudeAndLongitude(@RequestBody @Valid UserAddressInsert2 bean, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(bindingResult.getFieldError().getDefaultMessage());
        }
        UserAddressModel userAddressModel = new UserAddressModel();
        BeanUtils.copyProperties(bean, userAddressModel);
        return ResponseVoUtil.success(buyerAddressService.insert(userAddressModel));
    }

    @ACS(allowAnonymous = true)
    @ApiOperation(value = "更新", notes = "")
    @PutMapping("/update")
    public ResponseVO<UserAddressModel> update(@RequestBody @Valid UserAddressUpdate bean, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(bindingResult.getFieldError().getDefaultMessage());
        }
        UserAddressModel userAddressModel = new UserAddressModel();
        BeanUtils.copyProperties(bean, userAddressModel);
        // 解析地址为经纬度
        setGeocode(userAddressModel);
        return ResponseVoUtil.success(buyerAddressService.update(userAddressModel));
    }

    @ACS(allowAnonymous = true)
    @ApiOperation(value = "更新", notes = "")
    @PutMapping("/updateForLatitudeAndLongitude")
    public ResponseVO<UserAddressModel> updateForLatitudeAndLongitude(@RequestBody @Valid UserAddressUpdate2 bean, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(bindingResult.getFieldError().getDefaultMessage());
        }
        UserAddressModel userAddressModel = new UserAddressModel();
        BeanUtils.copyProperties(bean, userAddressModel);
        // 解析地址为经纬度
        return ResponseVoUtil.success(buyerAddressService.update(userAddressModel));
    }

    @ACS(allowAnonymous = true)
    @ApiOperation(value = "删除", notes = "")
    @DeleteMapping("/delete")
    public ResponseVO delete(@RequestBody UserAddressDeleteBean bean, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(bindingResult.getFieldError().getDefaultMessage());
        }
        UserAddressModel userAddressModel = buyerAddressService.get(bean.getId());
        if(userAddressModel == null){
            throw new BusinessException("地址不存在");
        }
        if(!userAddressModel.getUserWechatOpenid().equals(bean.getOpenId())){
            throw new BusinessException("地址不存在");
        }
        buyerAddressService.delete(userAddressModel);
        return ResponseVoUtil.success();
    }

    /**
     * 为用户地址DTO封装经纬度
     * @param userAddressModel
     * @return
     */
    private UserAddressModel setGeocode(UserAddressModel userAddressModel){
        Map location = mapService.addressToGeocoder(userAddressModel.getAreaName().concat(userAddressModel.getAddress()));
        userAddressModel.setLongitude(new BigDecimal((String) location.get("longitude")));
        userAddressModel.setLatitude(new BigDecimal((String) location.get("latitude")));
        return userAddressModel;
    }
}
